package com.yutian.user.demo.mapper;

import com.yutian.user.demo.dao.model.User;
import com.yutian.user.demo.dao.param.UserPageReqDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 1.使用xml来配置
 * 2.用script标签包围，然后像xml语法一样书写
 */
@Mapper
public interface UserMapper {
    @Select("SELECT id, name, stu_number from user WHERE id=#{id}")
    User get(@Param("id") long id);


    @Select("<script>"
            + "SELECT id, name, stu_number FROM user WHERE 1=1 "
            + "<if test=\"nameLike != null and nameLike != ''\"> AND name like CONCAT('%',#{nameLike},'%') </if>"
            + "</script>")
    List<User> list(@Param("nameLike") String nameLike);

    @Insert("insert into user (name, password, stu_number) values (#{t.name}, #{t.password}, #{t.stuNumber})")
    @Options(useGeneratedKeys = true, keyProperty = "t.id") // 注入t.id自增主键，返回行数
    long insert(@Param("t") User user);

    
    @Select("<script>"
            + "SELECT count(1) AS number FROM user WHERE 1=1 "
            + "<if test=\"req.name != null and req.name != ''\"> AND name=#{req.name} </if>"
            + "</script>")
    long count(@Param("req") UserPageReqDO req);

    @Select("<script>"
            + "SELECT id,name,msg FROM t_User WHERE 1=1 "
            + "<if test=\"req.name != null and req.name != ''\"> AND name=#{req.name} </if>"
            + "ORDER BY ${req.sortSql} "
            + "LIMIT #{req.offset}, #{req.pageSize} "
            + "</script>")
    List<User> page(@Param("req") UserPageReqDO req);

    /**
     * 或mappers/UserMapper.xml
     * @param user
     * @return
     */
    @Update("update user set name=#{req.name}, password=#{req.password}, stu_number=#{req.stuNumber} where id = #{req.id}")
    int update(@Param("req") User user);

    @Delete("delete from user where id = #{id}")
    int deleteById(@Param("id") Long id);
}